// project created on 11/2/2006 at 10:40 AM
using System;
using System.Collections.Generic;

namespace tvgrabau
{
	public static class MainClass
	{
		public static GrabberOptions options;
		private static Config config;
		public static ApplicationArguments ar;
		public static List<Programme> p = new List<Programme>();
		public static bool loud = true;
		public static int Width = 0;
		//public static int ReturnCode = -1;
		public static GrabberOptions ConfigureOptions = new GrabberOptions();
	
		public static void Main(string[] args)
		{
			Width = Console.BufferWidth / 2;
			if (Width == 0)
			{
				Width = Console.WindowWidth / 2;
			}
			
			string TVGADir = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + System.IO.Path.DirectorySeparatorChar + ".tvgrabau" + System.IO.Path.DirectorySeparatorChar;
			
			if (!System.IO.Directory.Exists(TVGADir))
			{
				System.IO.Directory.CreateDirectory(TVGADir);
			}
		
			// Grab our arguments
			ArgumentParser ap = new ArgumentParser(args, TVGADir);
			//ar.Config = TVGADir + "config.xml";
			ar = ap.ParseArguments();
			
			// And load up the config file
			config = new Config(ar.Config);
			if (ar.ExecMode == ApplicationArguments.RunMode.Configuration || ar.ExecMode == ApplicationArguments.RunMode.ShowHelp || ar.XMLTV_OutputMode == ApplicationArguments.XMLTV_Mode.Capabilities || ar.XMLTV_OutputMode == ApplicationArguments.XMLTV_Mode.Description || ar.ExecMode == ApplicationArguments.RunMode.Version)
			{
				config.IgnoreErrors = true;
			}
			
			options = config.loadConfiguration();
			
			// Show the bootstraper version information
			if (ar.Quiet || options.Quiet || ar.XMLTV_OutputMode == ApplicationArguments.XMLTV_Mode.Capabilities || ar.XMLTV_OutputMode == ApplicationArguments.XMLTV_Mode.Description)
			{
				loud = false;
			}
			
			// Do some funky stuff to our plugin name variable
			string pName = "";
			if (options.PluginName.Length > 0)
			{
				pName = options.PluginName;
			}
			
			if (ar.PluginName.Length > 0)
			{
				pName = ar.PluginName;
			}
			
			
			if (pName.EndsWith(".dll") == false && pName.Length > 0)
			{
				pName += ".dll";
			}
			
			if (pName.Length == 0)
			{
				// Quick, make it the default!
				pName = "tvgrabau-yourtv.dll";
			}
			
			if (loud)
			{
				Console.WriteLine("tvgrabau Bootstrapper v"+System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
			}
			
			// Create a new 'grabber' object..
			GrabberManager g = new GrabberManager(TVGADir);
			bool passed = g.selectPlugin(pName);
			if (passed)
			{
				GrabberManager.PluginInfo gInfo = g.GetInfo();
				if (loud)
				{
					Console.WriteLine("Using plugin: {0}, v{1}", gInfo.Name, gInfo.Version);
				}
			}
			
			
			// Work out what we want to do and do it!
			if (ar.ExecMode == ApplicationArguments.RunMode.Grab && ar.XMLTV_OutputMode == ApplicationArguments.XMLTV_Mode.None)
			{
				g.PerformFetch();
				OutputManager om = new OutputManager(p);
				if (loud)
				{
					Console.WriteLine();
					//Console.WriteLine("Going to write {0} programmes to file.", p.Count);
					Console.Write("Writing programmes to file... ");
				}
				
				om.WriteOutput(ar.OutputFilename);
				
				if (loud)
				{
					Console.WriteLine("Complete!");
				}
			}
			else if (ar.ExecMode == ApplicationArguments.RunMode.ShowHelp)
			{
				g.PrintHelp();
				return;
			}
			else if (ar.ExecMode == ApplicationArguments.RunMode.Configuration)
			{
				config = new Config(ar.Config);
				Console.WriteLine();	// Separate version info from prompts.
				g.Configure();
				config.writeConfiguration(ConfigureOptions);
				
			}
			else if (ar.ExecMode == ApplicationArguments.RunMode.ListChannels)
			{
				g.ListChannels();
			}
			else if (ar.ExecMode == ApplicationArguments.RunMode.Test)
			{
				//g.RunTestMode();
			}
			else if (ar.XMLTV_OutputMode == ApplicationArguments.XMLTV_Mode.Capabilities)
			{
				g.PrintCaps();
			}
			else if (ar.XMLTV_OutputMode == ApplicationArguments.XMLTV_Mode.Description)
			{
				g.PrintDesc();
			}
			else if (ar.ExecMode == ApplicationArguments.RunMode.Version)
			{
				//g.PrintVer();
			}
			
			// See you, Batman.
		}
	}
	
	public enum ProgrammeType
	{
		Errored,
		Movie,
		Series,
		Sports,
		Normal
	}
	
	public class Programme
	{				
			public string Title;
			public int ID = 0;
			public ProgrammeType Type = ProgrammeType.Normal;
			public string EpisodeName;
			public string Classification;
			public string Description;
			public DateTime TimeStart;
			public DateTime TimeEnd;
			public string Category;
			public string Country;
			public bool Widescreen;
			public bool Repeat;
			public bool Premiere;
			public bool Teletext;
			public bool HDTV;
			public string ChannelName;	
			public bool Final;
			public string Year;
			public string Cast;
			public string Score;
	}
}
